import 'package:flutter/material.dart';

import 'home_controller.dart';
import 'package:blog_chyk_intl/utils/index.dart';

class HomeView extends StatelessWidget {
  HomeView({super.key});

  // 找到控制器（自动生命周期绑定）
  final HomeController controller = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('文章列表'),
        actions: [
          Obx(
            () => IconButton(
              icon: Icon(isDarkMode ? Icons.light_mode : Icons.dark_mode),
              onPressed: toggleThemeGlobal,
            ),
          ),
        ],
      ),

      // 用 Obx 监听 controller.articles 变化
      body: Obx(
        () => RefreshIndicator(
          onRefresh: () => controller.refreshArticles(),
          child: controller.isLoading.value && controller.articles.isEmpty
              ? const Center(child: CircularProgressIndicator()) // 转圈
              : ListView.builder(
                  itemCount: controller.articles.length,
                  itemBuilder: (_, i) {
                    final item = controller.articles[i];
                    return ListTile(
                      title: Text(item.title),
                      subtitle: Text(item.summary),
                      trailing: Text('${item.date.day}/${item.date.month}'),
                      onTap: () =>
                          Get.toNamed('/article', arguments: item.id), // 预留跳转
                    );
                  },
                ),
        ),
      ),
      // 右下角刷新按钮（可选）
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print(
            "${controller.isLoading.value}===========================================",
          );
          controller.refreshArticles();
        },
        child: const Icon(Icons.refresh),
      ),
    );
  }
}
